import { toInteger } from 'lodash';
import { IShapeInfo } from '../types';

export const checkAllEntitiesExist = (
  entities: string[],
  shapeInfo: Map<string, IShapeInfo>,
) => {
  if (!entities || entities.length === 0) {
    return true;
  }
  if (!shapeInfo) {
    return false;
  }
  const allExist = entities.every((entity) => {
    // entity 可能的值 id/id:v0/id:e1/id:f2
    // 将 entity 分割成 id 和 v 和 index
    const ids = entity.split(':');
    if (ids.length === 1) {
      return shapeInfo.has(entity);
    } else if (ids.length === 2) {
      const id = ids[0];
      const typeAndIndex = ids[1];
      if (typeAndIndex.length > 0) {
        const type = typeAndIndex[0];
        const index = toInteger(typeAndIndex.substring(1));
        switch (type) {
          case 'v':
            return shapeInfo.get(id)?.vertexCount > index;
          case 'e':
            return shapeInfo.get(id)?.edgeCount > index;
          case 'f':
            return shapeInfo.get(id)?.faceCount > index;
        }
      }
    }
    return true;
  });
  return allExist;
};
